/*
 * @lc app=leetcode.cn id=238 lang=cpp
 *
 * [238] 除自身以外数组的乘积
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    vector<int> productExceptSelf(vector<int> &nums)
    {
        vector<int> ans(nums.size(), 0);

        auto it = find_if(nums.begin(), nums.end(), [](int i) -> bool
                          { return i == 0; });

        if (it == nums.end())
        {
            int tmp = 1;
            for (auto n : nums)
                tmp *= n;
            for (int i = 0; i < ans.size(); i++)
            {
                ans[i] = tmp / nums[i];
            }
        }
        else
        {
            int tmp = 1;
            for (auto i = nums.begin(); i < nums.end(); i++)
            {
                if(it != i)
                {
                    tmp *= (*i);
                }
            }
            if(tmp != 0)
            {
                *(ans.begin() + (it - nums.begin())) = tmp;
            }
        }
        return ans;
    }
};
// @lc code=end
